class TreeNode:
    def __init__(self, id, data):
        self.id = id
        self.data = data
        self.children = []

    def __repr__(self):
        return f"TreeNode(id={self.id}, data={self.data})"


def build_tree(nodes):
    node_dict = {node['id']: TreeNode(node['id'], node['data']) for node in nodes}
    root = None

    for node in nodes:
        parent_id = node['parent_id']
        if parent_id is None:
            root = node_dict[node['id']]
        else:
            parent_node = node_dict[parent_id]
            parent_node.children.append(node_dict[node['id']])

    return root


def traverse_tree(root):
    if root is None:
        return

    print(root.data)
    for child in root.children:
        traverse_tree(child)


# 示例数据
nodes = [
    {'id': 4, 'parent_id': 2, 'data': 'child1_1'},
    {'id': 2, 'parent_id': 1, 'data': 'child1'},
    {'id': 3, 'parent_id': 1, 'data': 'child2'},
    {'id': 1, 'parent_id': None, 'data': 'root'},
    {'id': 5, 'parent_id': 2, 'data': 'child1_2'},
]

# 构建树
root = build_tree(nodes)

# 遍历树
traverse_tree(root)
